﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Needle.Utils.Collections
{
    /// <summary>
    /// Class to simulate VB's indexed properties in C#.
    /// </summary>
    public class Indexer<TKey, TValue> : IHideObjectMembers
    {

        Func<TKey, TValue> _getValue;
        Action<TKey, TValue> _setValue;
        /// <summary></summary>
        public TValue this[TKey key]
        {
            get { return _getValue(key); }
            set { _setValue(key, value); }
        }
        /// <summary>
        /// Initializes a new instance of the Indexer class.
        /// </summary>
        /// <param name="getValue">the indexer setter</param>
        /// <param name="setValue"></param>
        public Indexer(Func<TKey, TValue> getValue, Action<TKey, TValue> setValue)
        {
            if (getValue == null)
                throw new ArgumentNullException("getValue", "getValue is null.");
            if (setValue == null)
                throw new ArgumentNullException("setValue", "setValue is null.");
            _getValue = getValue;
            _setValue = setValue;
        }
    }

    /// <summary>
    /// Class to simulate VB's indexed properties in C#.
    /// </summary>
    public class ReadOnlyIndexer<TKey, TValue> : IHideObjectMembers
    {

        private Func<TKey, TValue> _getValue;

        /// <summary></summary>
        public TValue this[TKey key]
        {
            get { return _getValue(key); }
        }

        /// <summary>
        /// Initializes a new instance of the Indexer class.
        /// </summary>
        /// <param name="getValue">the indexer setter</param>
        public ReadOnlyIndexer(Func<TKey, TValue> getValue)
        {
            if (getValue == null)
                throw new ArgumentNullException("getValue", "getValue is null.");
            _getValue = getValue;
        }
    }
}
